/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;

import java.util.List;
import model.pojo.Customer;
import model.pojo.DealSold;
import model.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author DucThien
 */
public class dealSoldDAO extends AbstractDAO<DealSold, String> {

    @Override
    protected Class getPOJOClass() {
        return DealSold.class;
    }
    public List<Customer> getListDealSoldByDealID(int deal_id){
        List<Customer> list = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "SELECT ct FROM DealSold ds , Customer ct WHERE ds.dealId=:dealID AND ct.customerId=ds.customerId";
            Query query = session.createQuery(hql);
            query.setInteger("dealID", deal_id);
            list = query.list();
            if(list.isEmpty() == true){
                return null;
            }       
        }catch(HibernateException ex){
            System.err.println(ex);
        }finally{
            session.close();
        }     
        return list;
    }
    
    public boolean checkCustomerExistsWithDeal(int deal_id , int customer_id){
        List<DealSold> list = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "FROM DealSold ds WHERE ds.dealId=:dealID AND ds.customerId=:customerID";
            Query query = session.createQuery(hql);
            query.setInteger("dealID", deal_id);
            query.setInteger("customerID", customer_id);
            list = query.list();
            if(list.isEmpty() == true){
                return false; // not exists
            }       
        }catch(HibernateException ex){
            System.err.println(ex);
        }finally{
            session.close();
        }     
        return true; // exists
    }
}
